/**
 * @(#) ReqForOthersMediator.as
 *
 * @date		2009. 03. 06.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	근무 - Mediator
 * 				근무관리 Presentation Logic을 관리 
 */
package com.skt.common_hr.teamworktime.view
{
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.teamworktime.TeamWorkTimeFacade;
	import com.skt.common_hr.teamworktime.model.DelWorkTimeProxy;
	import com.skt.common_hr.teamworktime.model.ReqVacationProxy;
	import com.skt.common_hr.teamworktime.model.vo.MemberVO;
	import com.skt.common_hr.teamworktime.view.components.DeleteWorkTimeView;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	public class DeleteWorkTimeMediator extends CommonMediator
	{
		public static const NAME:String = "DeleteWorkTimeMediator"; 
		public static const DELETED_WORKTIME:String = "deletedWorktime";
		public static const CANCELED_DELETE:String = "canceledDelete";
		
		private var currentKey:String;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function DeleteWorkTimeMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
 			view.addEventListener(DeleteWorkTimeView.CONFIRM_DELETE, onDeleteWorkTime);
 			view.addEventListener(DeleteWorkTimeView.CANCEL, onClickCancel);
  		}
		
 		public function exec(member:MemberVO, key:String):void
 		{
 			view.selectedMember=member;
 			currentKey = key;
			loadWorkTime(currentKey); 			
 		}
 		
 		public function loadWorkTime(key:String):void
 		{
 			var proxy:ReqVacationProxy = facade.retrieveProxy(ReqVacationProxy.NAME) as ReqVacationProxy;
 			proxy.load(key);
 			trace(key);
 		}
 		

		private function onDeleteWorkTime(event:Event):void
		{
			CommonAlert.showMsgYesNo(commonFacade.bundle, "ALERT_PROMPT_DELETE", null, "확인", null, onConfirmDelete, onCancelDelete);
		}

		private function onClickCancel(event:Event):void
		{
			PopUpManager.removePopUp(view);	
			facade.sendNotification(CANCELED_DELETE, this);
		}
		
		private function onConfirmDelete():void
		{
			var proxy:DelWorkTimeProxy = facade.retrieveProxy(DelWorkTimeProxy.NAME) as DelWorkTimeProxy;
			proxy.deleteWorkTime(currentKey);
		}

		private function onCancelDelete():void
		{
			
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					TeamWorkTimeFacade.LOAD_PROFILE_COMPLETE,
					TeamWorkTimeFacade.LOAD_PROFILE_FAILED,
					TeamWorkTimeFacade.LOAD_REQ_VACATION_COMPLETE,
					TeamWorkTimeFacade.LOAD_REQ_VACATION_FAILED,
					DelWorkTimeProxy.DELETE_WORKTIME_COMPLETE,
					DelWorkTimeProxy.DELETE_WORKTIME_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case TeamWorkTimeFacade.LOAD_PROFILE_COMPLETE:
					//view.selectedMember = (note.getBody() as ProfileProxy).profile;
					break;
				case TeamWorkTimeFacade.LOAD_REQ_VACATION_COMPLETE:
					var vacationList:ArrayCollection = ReqVacationProxy(note.getBody()).reqVacationList;
					if(vacationList.length>0) {
						view.reqWorkTimeList = vacationList;
						if(vacationList.getItemAt(0).DGBN=="1") { //휴가이면
							view.vsReq.selectedIndex = 0;
						}
						else {	//근태
							view.vsReq.selectedIndex = 1;
						}
					}
					break;
				case DelWorkTimeProxy.DELETE_WORKTIME_COMPLETE:
					PopUpManager.removePopUp(view);
					facade.sendNotification(DELETED_WORKTIME, this);
					break;
				case DelWorkTimeProxy.DELETE_WORKTIME_FAILED:
					trace("failed to delete");
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():DeleteWorkTimeView
        {
            return viewComponent as DeleteWorkTimeView;
        }
	}
}